package com.lut.edu.apm.payCenter.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lut.edu.apm.payCenter.dao.UserOrderMapper;
import com.lut.edu.apm.payCenter.pojo.UserOrder;
import com.lut.edu.apm.payCenter.services.UserOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zhao Suzhen
 * @date `2024/4/15`
 */
@Service
public class UserOrderServiceImpl extends ServiceImpl<UserOrderMapper, UserOrder> implements UserOrderService {

    @Autowired
    UserOrderMapper userOrderMapper;

    @Override
    public boolean modifyStatus(String outTradeNo, int status) {
        return userOrderMapper.updateStatus(outTradeNo,status);
    }

    @Override
    public List<Map<String, Object>> queryOrderCountByDay() {
        LocalDate today = LocalDate.now();
        LocalDateTime startOfToday = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime endOfToday = LocalDateTime.of(today, LocalTime.MAX);

        QueryWrapper<UserOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("gmt_created", startOfToday, endOfToday)
                .eq("status", 1)
                .orderByAsc("gmt_created");
        List<UserOrder> userOrders = userOrderMapper.selectList(queryWrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (UserOrder userOrder : userOrders) {
            HashMap<String, Object> item = new HashMap<>();
            item.put("gmt_created", userOrder.getGmtCreated());
            result.add(item);
        }
        return result;
    }

    @Override
    public List<UserOrder> queryUserOrder(String userId, LocalDateTime time) {
        LocalDate today = LocalDate.now();
        LocalDateTime endOfToday = LocalDateTime.of(today, LocalTime.MAX);
        QueryWrapper<UserOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("gmt_created", time, endOfToday)
                .eq("user_id", userId)
                .orderByDesc("gmt_created");
        return userOrderMapper.selectList(queryWrapper);
    }
}
